define(function(require, exports, module) {
	//lib工具包
	require('resources/js/jquery.json-2.4.min.js');
	require('avalon2');
	//公用逻辑
	var resConstants=require('resources/js/rescatalogMix/res_constants.js');
	
	var ResInfoOperate=function(params){
		//默认值
		this.curResId=-1;
		this.resInfos={};	//<resId,resInfo>
		this.resInfoCmp={};	//<resId,ResInfoSchemaOperate,ServiceOperatePlugin>
		//配置
		$.extend(true,this,params);
		this.init();
	};
	
	ResInfoOperate.prototype={
		urls:{
			
		},
		init:function(){
			this.render();
			this.bind();
			this.sync();
			
			
		},
		render:function(){
			this.$div=$('#formContainer');	
		},
		bind:function(){
			var that=this;
			this.$div.on('click','.delete-form',function(){
				var resId=$(this).attr('resId');
				delete that.resInfos[resId];
				that.resInfoCmp[resId].destroy();
				delete that.resInfoCmp[resId];
			});
			$('#addSchemaResInfo').on('click',function(){
				that.addSchemaResInfo();
			});
			$('#addServiceResInfo').on('click',function(){
				that.addServiceResInfo();
			});
			$('#btn-save').on('click',function(){
				that.submit('save');
			});	
		},
		addSchemaResInfo:function(resInfo){
			var ResInfoSchemaOperate=require('resources/template/resInfo/resInfo_schema_operate.js'),
				resId=this.curResId--,
				$formContainer=this.$div;
			if(resInfo==undefined)
				resInfo={
					resId:resId,
					bizName:'',
					resProviderName:this.org.name,
					resProviderCode:this.org.code,
					resProvider:this.org.id,
					resType:1,
					carrierType:'',	//一定要写上，否则无法实现双向绑定
					resTypeName:resConstants.resType[1],
					schema:{
						schemaId:resId,
						entries:[]
					}	
				};
			this.resInfos[resId]=resInfo
			this.resInfoCmp[resId]=new ResInfoSchemaOperate(resInfo,$formContainer,this.getCmpConfig());
		},
		getCmpConfig:function(){
			return {
				baseUrl:this.baseUrl,
				step:'draft',
				selfDelete:true
			};
		},
		addServiceResInfo:function(resInfo){
			var ServiceOperatePlugin=require('resources/template/resInfo/resInfo_service_editor.js'),
				resId=this.curResId--,
				$formContainer=this.$div;
			if(resInfo==undefined)
				resInfo={
					resId:resId,
					bizName:'',
					resProviderName:this.org.name,
					resProviderCode:this.org.code,
					resProvider:this.org.id,
					resType:2,
					carrierType:'',	//一定要写上，否则无法实现双向绑定
					resTypeName:resConstants.resType[2],
					service:{
						serviceId:resId,
						serviceType:'',
						serviceMemo:''
					}
				};
			this.resInfos[resId]=resInfo
			this.resInfoCmp[resId]=new ServiceOperatePlugin(resInfo,$formContainer,this.getCmpConfig());
		},
		sync:function(){
			
		},
		urls:{
			edit:{
				save:'/resPublish/updateSave.jhtml',
			},
			create:{
				save:'/resPublish/createSave.jhtml',
			}
		},
		getUrl:function(action){
			if(this.urls[this.operateCode][action])
				return this.baseUrl+this.urls[this.operateCode][action];
			else if(this.urls[action])
				return this.baseUrl+this.urls[action];
			else 
				return this.baseUrl+action;
		},
		submit:function(action,param){
			
		}
	};
	
	return ResInfoOperate;
});	